<?php
class User extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('services/user_service', 'user_service');
    }

    public function userCenter()
    {
        $person_id = $this->request_array['personId'] ?? null;
        $err = null;
        $user_info = $this->user_service->getUserCenter($this->_uid, $person_id, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson(STATUS_OK, $user_info);
    }

    public function changeUserInfo()
    {
        $update_data = [
            'user_name' => $this->request_array['userName'] ?? null,
            'headpic' => $this->request_array['headpic'] ?? null,
        ];
        $err = null;
        $change = $this->user_service->changeUserInfo($update_data, $this->_uid, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson();
    }

    public function getConsumptionLog()
    {
        $page = $this->request_array['page'] ?? 1;
        $err = null;
        $consumption_log = $this->user_service->getConsumptionLog($this->_uid, $page, $err);
        if ( ! is_null($err))
        {
            $this->renderJson($err['errorCode'], $err['errorMessage']);
        }
        $this->renderJson(STATUS_OK, $consumption_log);
    }

    public function logOut()
    {
        $this->user_service->logout($this->_uid);
        $this->renderJson();
    }
}